from openpyxl.chart import (
    PieChart,
    ProjectedPieChart,
    Reference
)
from openpyxl.chart.series import DataPoint
from openpyxl import Workbook

# 创建工作表
wb = Workbook()
ws = wb.active

data = [
    ['Page', 'Views'],
    ['Search', 95],
    ['Products', 4],
    ['Offers', 0.5],
    ['Sales', 0.5],
]

# 向工作表中添加数据
for row in data:
    ws.append(row)
# 创建饼图
projected_pie = ProjectedPieChart()
# 饼图类
projected_pie.type = "pie"
# 分割类型
projected_pie.splitType = "val"  # split by value
# 标签
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
# 数据
data = Reference(ws, min_col=2, min_row=1, max_row=5)
# 添加数据
projected_pie.add_data(data, titles_from_data=True)
# 设置分类
projected_pie.set_categories(labels)
# 添加到工作表
ws.add_chart(projected_pie, "A10")

from copy import deepcopy

projected_bar = deepcopy(projected_pie)
projected_bar.type = "bar"
projected_bar.splitType = 'pos'  # split by position

ws.add_chart(projected_bar, "A27")

wb.save("pie2.xlsx")
